package com.net355.eblog.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class BeansFactory implements ApplicationContextAware {
	private static Log log = LogFactory.getLog(BeansFactory.class);

	private static ApplicationContext appContext;

	@Override
	public void setApplicationContext(ApplicationContext appContext) throws BeansException {
		System.out.println("result-->11");
		BeansFactory.appContext = appContext;
	}

	private static ApplicationContext getApplicationContext() {
		System.out.println("result-->22");
		return appContext;
	}

	/**
	 * @param name
	 * @return
	 */
	public static Object get(String name) {
		Object o = null;
		try {
			o = getApplicationContext().getBean(name);
		} catch (Exception e) {
			log.error("Load Spring bean " + name + " error--->>>", e);
		}
		return o;
	}

	/**
	 * @param name
	 * @return
	 */
	public static Object get(Class<?> cls) {
		Object o = null;
		try {
			o = getApplicationContext().getBean(cls);
		} catch (Exception e) {
			log.error("Get bean " + cls.getName() + " error--->>>", e);
		}
		return o;
	}
}
